/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
user = null;
 curso = null;
 wwwroot = null;
$(document).ready(function() {
   $.mobile.page.prototype.options.addBackBtn=true;
   $.mobile.page.prototype.options.domCache = true;
  // pageContainerElement.page({ domCache: true });
 $('input#login-btn').live('click', function(e) {
     
          $.mobile.showPageLoadingMsg();
          if( validar_datos( "form-login" ) ){
                              
                 $.ajax({
                        contentType: "application/x-www-form-urlencoded",
                        type: "POST",       
                        url: "lib/lib.php",
                        data: $( "#login-form" ).serialize(),
                        dataType: "json",
                        async:false,           
                        success: function( resp ){
                            
                               user =  eval ( resp );
                               
                            wwwroot = user.wwwroot;
                              if( user.error == "none"){

                                 //show_muro( user[user.userid] )
                                 window.location.replace("index.php");
                              }else{

                                   window.location.replace("#error");
                              }                        
                        },//end success
                        error:function(xhr, ajaxOptions, thrownError){
                            alert(xhr.status + " "+ thrownError);
                             $.mobile.hidePageLoadingMsg();
                        }
                  });
             
            }else{
                $.mobile.hidePageLoadingMsg();
              alert("todos los campos son necesarios");
            }                
    });//end click
 
 
   $('a.courseview').live('click', function(e) {
          var courseid = $(this).attr("id").split("_");
            /*$.mobile.changePage( "course.php", {
                    type: "post", 
                    data: "courseid="+courseid[1],
                    //dataUrl:  "course.php",
                    reloadPage: true,
                    fromHashChange:true,
                    reverse: true
                    //pageContainer: "http://rsa.utpl.edu.ec/eva1/m/course/"
            });
          e.preventDefault();*/
       
          $.ajax({
                        contentType: "application/x-www-form-urlencoded",
                        type: "POST",       
                        url: "course/course.php",
                        data: "courseid="+courseid[1]+"&oper=get_curso",                     
                        async:false,           
                        success: function( resp ){
                                 //alert(resp);
                                 //$.mobile.showPageLoadingMsg();
                                 $( "#content" ).append( resp );
                                 $.mobile.hidePageLoadingMsg();
                                 window.location.replace("#menucourse");
                                  
                        },//end success
                        error:function(xhr, ajaxOptions, thrownError){
                            alert(xhr.status + " "+ thrownError);
                             $.mobile.hidePageLoadingMsg();
                        }
                  });
       /*alert("entras");
       $.mobile.loadPage( "course/proba.php", {
           pageContainer:$("#coursemen"),
            type: "post",
            data: "courseid="+courseid[1]
       });*/
       
       
   });//end click courseview
   
  /* $('a.link_interno').live('click', function(e) {
          var pag = $(this).attr("id").split("_");
            /*alert(pag[1]);
            $.mobile.changePage( "course.ph#"+pag[1],  "slide", {reloadPage: true,
                dataUrl:  "course/course.php#"+pag[1]
            });**/
            //alert("opcion2");
            //window.location.replace("http://rsa.utpl.edu.ec/eva1/m/course/course.php#"+pag[1]);
            //alert(wwwroot);
            
            
          
   //});//end click courseview*/
 
   /* $(document).bind( "pagebeforechange", function( e, data ) {
        //alert(data.toPage);
        if ( typeof data.toPage === "string" ) {
            //alert( data.toPage );
            if( data.toPage.search("course/course.php") !== -1 ){
                
            }
        }
      
	//show_muro( user[user.userid], data );
	//e.preventDefault();
    }); //end pagebeforechange*/
 
    
    
 
 /*$("div[id*='anuncios']").live('pageshow', function(event, ui) { 

              alert("Desdee el envento");

            });
 */
 
});//end document.ready


function validar_datos( section ){
    var ban = false;
    if( section == "form-login" ){
        if( $("#username").val() != "" && $("#password").val() != ""  ){
            ban = true;
        }
    }//end section
    
    return ban;
}//ed validar_datos

/*function show_muro( userinfo ){
 
  var $page = $( "#muro" );
  $header = $page.children( ":jqmData(role=header)" );		
  $content = $page.children( ":jqmData(role=content)" );
  $header.find( "h1" ).html( userinfo.name );
  
              
  
  var markup = '<ul data-role="listview" data-inset="true" data-count-theme="e">'                             
                  +'           <li data-role="list-divider">Bienvenido</p></li>'
                  +'           <div class="ui-grid-a">'
                  +'                  <div style="width: 15%" class="ui-block-a">'
                  +'   <img src="'+ userinfo.picture + '" alt="Usuario picture"/>'
                  +'                  </div>'
                  +'                  <div style="width: 75%" class="ui-block-b">'
                  +'                      <strong>'+ userinfo.name +'</strong>'
                  +'                      <h2>'+ userinfo.city +'</h2>'
                  +'                  </div>'
                  +'          </div><!-- /grid-a -->'
                  +'           <li><a href="#"><img height="25px" width="25px" src="imgs/user_info.png" alt="Info User" class="ui-li-icon">Perfil</a></li>' 
                  +'           <li><a data-transition="slide" href="#mensajes"><img height="25px" width="25px" src="imgs/mail.png" alt="Info User" class="ui-li-icon">  Mensajes<span class="ui-li-count">'+ userinfo.no_mesg +'</span></a></li>' 
                  +'           <li><a data-transition="slide" href="./course/course.php"><img height="25px" width="25px" src="imgs/course_icon.png" alt="Info User" class="ui-li-icon">Cursos<span class="ui-li-count">'+  userinfo.no_cursos +'</span></a>'
                  +'           </li>'
                  +'</ul>'
   
   $content.html( markup );           
  $page.page();			
  $content.find( ":jqmData(role=listview)" ).listview();
  
}//end show muro*/